﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data ;
using System .Data .SqlClient ;
using dhDataAcess.dataAcess .sysDataSetTableAdapters ;
using dhDataAcess.Model ;

namespace dhDataAcess.sysDAL
{
   public  class companyinfoDAL
    {
       string tableName = "companyinfo";
          private SqlTransaction tran = null;
        private SqlConnection conn = null;
        public companyinfoDAL(SqlTransaction _transaction, SqlConnection _conn)
        {
            tran = _transaction;
            conn=_conn;
        }
      
        /// <summary>
        /// 插入一行公司信息表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>

        public int companyinfoInsert(companyinfoModel deptRow)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string insertStr = baseBLL.dTableColumnRead.insertSqlStr(tableName).ToString();
            //string insertStr = "insert into companyinfo (mark,mobile,email,linkman,address,fax,telPhone,cnameByEN,cname) values(@mark,@mobile,@email,@linkman,@address,@fax,@telPhone,@cnameByEN,@cname)";
            SqlParameter[] para = {
                                  new SqlParameter ("@mark",deptRow .mark),
                                  new SqlParameter ("@mobile",deptRow .mobile),
                                  new SqlParameter ("@email",deptRow .email ),
                                  new SqlParameter ("@linkman",deptRow .linkman ),
                                  new SqlParameter ("@address",deptRow .address ),
                                  new SqlParameter ("@fax",deptRow .fax ),
                                  new SqlParameter ("@telPhone",deptRow .telPhone ),
                                  new SqlParameter ("@cnameByEN",deptRow .cnameByEN),
                                  new SqlParameter ("@cname",deptRow .cname)
                                 };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = insertStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
           
        }

        /// <summary>
        /// 更新一行公司信息表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>
 
        public int companyinfoUpdate(companyinfoModel deptRow)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string updateStr = baseBLL.dTableColumnRead.updateSqlStr(tableName).ToString();
           // string updateStr = "update companyinfo set mark=@mark,mobile=@mobile,email=@email,linkman=@linkman,address=@address,fax=@fax,telPhone=@telPhone,cnameByEN=@cnameByEN where cname=@cname";
            SqlParameter[] para = {
                                  new SqlParameter ("@recordid",deptRow .recordid ),
                                  new SqlParameter ("@mark",deptRow .mark),
                                  new SqlParameter ("@mobile",deptRow .mobile),
                                  new SqlParameter ("@email",deptRow .email ),
                                  new SqlParameter ("@linkman",deptRow .linkman ),
                                  new SqlParameter ("@address",deptRow .address ),
                                  new SqlParameter ("@fax",deptRow .fax ),
                                  new SqlParameter ("@telPhone",deptRow .telPhone ),
                                  new SqlParameter ("@cnameByEN",deptRow .cnameByEN),
                                  new SqlParameter ("@cname",deptRow .cname)
                                  };
           
            cmd.Parameters.AddRange(para);
            cmd.CommandText = updateStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }


        /// <summary>
        /// 删除一行公司信息表数据
        /// </summary>
        /// <param name="cname"></param>
        /// <returns></returns>

        public int companyinfoDel(string recordid)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string deleteStr = baseBLL.dTableColumnRead.delSqlStr(tableName).ToString();
           // string deleteStr = "delete companyinfo where cname=@cname";
            SqlParameter[] para = {
                                new SqlParameter ("@recordid",recordid)
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = deleteStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 根据电话返回公司名
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>

        public string companyinforetcname(string mobile)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string selectStr = "selete cname from basList where mobile=@mobile";
            SqlParameter[] para = {
                                new SqlParameter ("@mobile",mobile),
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = selectStr;
            cmd.Transaction = tran;
            SqlDataReader rdr = cmd.ExecuteReader();
            string cname = null;
            while (rdr.Read())
            {
                cname = rdr[0].ToString();
            }

            return cname;
        }
    }
}
